using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using Barbar.MapReport.Common;

namespace Barbar.MapReport.HtmlRender {
  internal static class Utility {
    public static XmlSerializerNamespaces EmptyXmlNamespaceSerializer;

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
    static Utility() {
      EmptyXmlNamespaceSerializer = new XmlSerializerNamespaces();
      EmptyXmlNamespaceSerializer.Add(String.Empty, String.Empty);
    }

    public static string GetEmbeddedResource(string resourceName, bool addDefaultResourceFolderPrefix) {
      string name;
      if (addDefaultResourceFolderPrefix)
        name = "Barbar.MapReport.HtmlRender.Resources." + resourceName;
      else
        name = resourceName;

      Assembly assembly = Assembly.GetExecutingAssembly();
      string result = null;
      using (Stream stream = assembly.GetManifestResourceStream(name)) {
        if (stream == null)
          throw MapReportException.CreateException("Unable do locate resource '{0}' in current assembly.", name);
        using (StreamReader reader = new StreamReader(stream)) {
          result = reader.ReadToEnd();
          reader.Close();
        }
        stream.Close();
      }
      return result;
    }

    public static string Replace(string value, List<KeyValuePair<string, string>> replaceTable) {
      string result = value;
      foreach(KeyValuePair<string, string> keyValuePair in replaceTable) {
        result = result.Replace(keyValuePair.Key, keyValuePair.Value);
      }
      return result;
    }
  }
}
